package org.permission.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.shiro.crypto.hash.Md5Hash;
import org.permission.dao.SysPermissionDao;
import org.permission.dao.SysUserDao;
import org.permission.pojo.SysPermission;
import org.permission.pojo.SysUser;
import org.permission.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/sysUser")
public class SysUserController {
	@Autowired
	private SysUserDao sysUserDao;
	@Autowired
	private SysPermissionDao sysPermissionDao;
	
	
	public void setSysPermissionDao(SysPermissionDao sysPermissionDao) {
		this.sysPermissionDao = sysPermissionDao;
	}

	public void setSysUserDao(SysUserDao sysUserDao) {
		this.sysUserDao = sysUserDao;
	}
	
	@RequestMapping("/all")
	public List<SysUser> searchAll() throws Exception {
		return sysUserDao.getAll();
	}
	
	@RequestMapping("/index")
	public String index() throws Exception {
		return "sysUser/index";
	}
	
	@RequestMapping("/list")
	@ResponseBody
	public Map<String, Object> list(@RequestParam(defaultValue="1") Integer page,@RequestParam(defaultValue="10") Integer rows,
			@RequestParam(defaultValue="asc") String order,@RequestParam(defaultValue="id") String sort,
			SysUser condition) throws Exception {
		Map<String, Object> map = new HashMap<>();
		
		int start = (page - 1) * rows;
		List<SysUser> list = sysUserDao.getConditionList(start, rows, condition, sort, order);
		int total = sysUserDao.getConditionCount(condition);
		
		map.put("total", total); 
		map.put("rows", list);
		
		return map;
	}
	
	
	@RequestMapping(value="/form",method=RequestMethod.GET)
	public String add() throws Exception {
		return "sysUser/form";
	}
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> add(SysUser sysUser,Integer[] roleIds) throws Exception {
		Map<String, Object> map = new HashMap<>();
		Md5Hash md5Hash = new Md5Hash(sysUser.getPassword(),sysUser.getSalt());
		sysUser.setPassword(md5Hash.toString());
		sysUserDao.add(sysUser);
		sysUserDao.addUserRoles(sysUser.getId(),roleIds);
		map.put("result", true);
		return map;
	}
	
	@RequestMapping(value="/batchDelete")
	@ResponseBody
	public Map<String, Object> batchDelete(Integer[] ids) throws Exception {
		Map<String, Object> map = new HashMap<>();
		sysUserDao.batchDelete(ids);
		map.put("result", true);
		return map;
	}
	
	@RequestMapping("/view/{id}")
	@ResponseBody
	public SysUser view(@PathVariable Integer id) throws Exception {
		return sysUserDao.get(id);
	}
	
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> edit(SysUser sysUser) throws Exception {
		System.out.println(sysUser);
		Map<String, Object> map = new HashMap<>();
		sysUserDao.update(sysUser);
		map.put("result", true);
		return map;
	} 
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String login() throws Exception {
		return "login";
	}
	
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public String login(String username,String password,HttpSession session) throws Exception {
		SysUser sysUser = sysUserDao.getByUserName(username);
		if(sysUser == null) {
			session.setAttribute("error","用户名或者密码错误！");
			return "redirect:/sysUser/login";
		}
		String salt = sysUser.getSalt();
		Md5Hash md5Hash = new Md5Hash(password,salt);
		if(!md5Hash.toString().equals(sysUser.getPassword())) {
			session.setAttribute("error","用户名或者密码错误！");
			return "redirect:/sysUser/login";
		}
		
		session.setAttribute(Constant.LOGIN_USER, sysUser);
		//根据用户的角色查询对应的权限信息，放到作用域中
		List<SysPermission> perList = sysPermissionDao.getPersByUserId(sysUser.getId());
		session.setAttribute(Constant.PERMISSION_LIST, perList);
		
		
		return "redirect:main";
	}
	@RequestMapping("/main")
	public String main() throws Exception{
		return "main";
	}
	
	@RequestMapping("/logout")
	public String logout(HttpSession session) throws Exception {
		session.removeAttribute(Constant.LOGIN_USER);
		return "redirect:/sysUser/login";
	}
}
